package com.sg.service.biz.operation.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.operation.req.QueryBlockchainCertificationListReq;
import com.sg.dto.biz.operation.req.*;
import com.sg.dto.biz.operation.res.*;
import com.sg.entity.OmsOperationBlockchainCertificate;
import com.sg.service.base.operation.MOmsOperationBlockchainCertificateService;
import com.sg.service.biz.operation.nb.NbBlockchain;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class BlockchainServiceImpl
        implements com.wicket.okroperation.biz.service.BlockchainService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MOmsOperationBlockchainCertificateService mOmsOperationBlockchainCertificateService;
    @Resource
    private NbBlockchain nbBlockchain;
//@Resource
    //private MMmmShellService mMmmShellService;

    /**
     * D6新增区块链哈希值(公共)[6990]
     * gen by moon at 5/30/2023, 6:57:22 PM
     */
    @Trace(operationName = "D6新增区块链哈希值(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddHashValueComRespDto addHashValueCom(AddHashValueComReqDto reqDto) {


        AddHashValueRespDto addHashValueRes_1 = null;
//步骤0: M6新增区块链哈希值（特殊方法） - addHashValue
        //ModelCode: addHashValue
        AddHashValueRespDto addHashValueRes = null;
        AddHashValueReqDto addHashValueReq = new AddHashValueReqDto();
        if (reqDto != null) {
            addHashValueReq.setDepositData(reqDto.getDepositData());//SimpleFieldAssign//sourceId:989106_1
            addHashValueReq.setBlockchainKey(reqDto.getBlockchainKey());//SimpleFieldAssign//sourceId:989103_1
            addHashValueReq.setBlockchainSecret(reqDto.getBlockchainSecret());//SimpleFieldAssign//sourceId:989104_1
        }

        /*M6新增区块链哈希值（特殊方法）[6988]   */
        Assert.isNull(addHashValueReq.getDepositData(), "D6新增区块链哈希值(公共)-M6新增区块链哈希值（特殊方法）-存证数据不能为空", false);
        Assert.isNull(addHashValueReq.getBlockchainKey(), "D6新增区块链哈希值(公共)-M6新增区块链哈希值（特殊方法）-上链KEY不能为空", false);
        Assert.isNull(addHashValueReq.getBlockchainSecret(), "D6新增区块链哈希值(公共)-M6新增区块链哈希值（特殊方法）-上链密钥不能为空", false);
        addHashValueRes = nbBlockchain.addHashValue(addHashValueReq);


        addHashValueRes_1 = addHashValueRes;

        AddHashValueComRespDto retData = new AddHashValueComRespDto();
        if (addHashValueRes_1 != null) {
            retData.setHashValue(addHashValueRes_1.getHashValue());//SimpleFieldAssign//sourceId:989110_1
            retData.setBlockchainDataUnicode(addHashValueRes_1.getBlockchainDataUnicode());//SimpleFieldAssign//sourceId:989444_1
        }


        return retData;
    }

    /**
     * D6新增存证数据证书(公共)[6989]
     * gen by moon at 5/28/2023, 11:42:53 PM
     */
    @Trace(operationName = "D6新增存证数据证书(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddDataDepositCertificationComRespDto addDataDepositCertificationCom(AddDataDepositCertificationComReqDto reqDto) {


        GenerateDataDepositCertificationRespDto dataDepositCertificationRes_1 = null;
//步骤0: M6生成存证数据证书（特殊方法） - generateDataDepositCertification
        //ModelCode: dataDepositCertification
        GenerateDataDepositCertificationRespDto dataDepositCertificationRes = null;
        GenerateDataDepositCertificationReqDto dataDepositCertificationReq = new GenerateDataDepositCertificationReqDto();
        if (reqDto != null) {
            dataDepositCertificationReq.setContentDress(reqDto.getContentDress());//SimpleFieldAssign//sourceId:989035_1
        }

        /*M6生成存证数据证书（特殊方法）[6987]   */
        Assert.isNull(dataDepositCertificationReq.getContentDress(), "D6新增存证数据证书(公共)-M6生成存证数据证书（特殊方法）-内容地址不能为空", false);
        dataDepositCertificationRes = nbBlockchain.generateDataDepositCertification(dataDepositCertificationReq);


        dataDepositCertificationRes_1 = dataDepositCertificationRes;

        AddDataDepositCertificationComRespDto retData = new AddDataDepositCertificationComRespDto();
        if (dataDepositCertificationRes_1 != null) {
            retData.setDepositData(dataDepositCertificationRes_1.getDepositData());//SimpleFieldAssign//sourceId:989038_1
        }


        return retData;
    }

    /**
     * D6新增区块链证书[6993]
     * gen by moon at 5/30/2023, 7:09:57 PM
     */
    @Trace(operationName = "D6新增区块链证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddBlockchainCertificationRespDto addBlockchainCertification(AddBlockchainCertificationReqDto reqDto) {


        String string_1 = null;
//步骤0: 6-1-02新增区块链证书 - addBlockchainCertification
        String string = null;
        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate = new OmsOperationBlockchainCertificate();
        if (reqDto != null) {
            omsOperationBlockchainCertificate.setCertificateName(reqDto.getCertificateName());//SimpleFieldAssign//sourceId:989297_1
            omsOperationBlockchainCertificate.setCertificateCode(reqDto.getCertificateCode());//SimpleFieldAssign//sourceId:989298_1
            omsOperationBlockchainCertificate.setCertificateDesc(reqDto.getCertificateDesc());//SimpleFieldAssign//sourceId:989299_1
            omsOperationBlockchainCertificate.setDepositData(reqDto.getDepositData());//SimpleFieldAssign//sourceId:989300_1
            omsOperationBlockchainCertificate.setHashValue(reqDto.getHashValue());//SimpleFieldAssign//sourceId:989301_1
            omsOperationBlockchainCertificate.setShortHash(reqDto.getShortHash());//SimpleFieldAssign//sourceId:989302_1
            omsOperationBlockchainCertificate.setCertificateStartTime(reqDto.getCertificateStartTime());//SimpleFieldAssign//sourceId:989303_1
            omsOperationBlockchainCertificate.setCopyrightOwner(reqDto.getCopyrightOwner());//SimpleFieldAssign//sourceId:989304_1
            omsOperationBlockchainCertificate.setAuthorizationSpaceId(reqDto.getAuthorizationSpaceId());//SimpleFieldAssign//sourceId:989305_1
            omsOperationBlockchainCertificate.setSurfaceCode(reqDto.getSurfaceCode());//SimpleFieldAssign//sourceId:989306_1
            omsOperationBlockchainCertificate.setEntityId(reqDto.getEntityId());//SimpleFieldAssign//sourceId:989307_1
            omsOperationBlockchainCertificate.setThemeContentTypeCode(reqDto.getThemeContentTypeCode());//SimpleFieldAssign//sourceId:989308_1
            omsOperationBlockchainCertificate.setThemeContentId(reqDto.getThemeContentId());//SimpleFieldAssign//sourceId:989309_1
            omsOperationBlockchainCertificate.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:989310_1
        }

        /*6-1-02新增区块链证书[6992]   */
        Assert.isNull(omsOperationBlockchainCertificate.getCertificateName(), "D6新增区块链证书-6-1-02新增区块链证书-证书名称不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getCertificateCode(), "D6新增区块链证书-6-1-02新增区块链证书-证书标识不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getCertificateDesc(), "D6新增区块链证书-6-1-02新增区块链证书-证书简介不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getDepositData(), "D6新增区块链证书-6-1-02新增区块链证书-存证数据不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getHashValue(), "D6新增区块链证书-6-1-02新增区块链证书-哈希值不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getShortHash(), "D6新增区块链证书-6-1-02新增区块链证书-短哈希不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getCertificateStartTime(), "D6新增区块链证书-6-1-02新增区块链证书-存证开始时间不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getCopyrightOwner(), "D6新增区块链证书-6-1-02新增区块链证书-版权归属方不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getAuthorizationSpaceId(), "D6新增区块链证书-6-1-02新增区块链证书-租户空间ID不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getSurfaceCode(), "D6新增区块链证书-6-1-02新增区块链证书-关联内容表编码不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getEntityId(), "D6新增区块链证书-6-1-02新增区块链证书-关联内容表主键ID不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getThemeContentTypeCode(), "D6新增区块链证书-6-1-02新增区块链证书-主题内容类型编码不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getThemeContentId(), "D6新增区块链证书-6-1-02新增区块链证书-主题内容ID不能为空", false);
        Assert.isNull(omsOperationBlockchainCertificate.getSubjectLifeCycle(), "D6新增区块链证书-6-1-02新增区块链证书-主体生命周期不能为空", false);
        string = mOmsOperationBlockchainCertificateService.addBlockchainCertification(omsOperationBlockchainCertificate)/*vcase invoke 本地 method 方法调用;*/;


        string_1 = string;

        AddBlockchainCertificationRespDto retData = new AddBlockchainCertificationRespDto();
        if (string_1 != null) {
            retData.setBlockchainCertificateId(string_1);//SimpleFieldAssign//sourceId:989328_1
        }


        return retData;
    }

    /**
     * D6查询区块链证书列表[6997]
     * gen by moon at 5/30/2023, 7:09:59 PM
     */
    @Trace(operationName = "D6查询区块链证书列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBlockchainCertificationListRespDto queryBlockchainCertificationList(QueryBlockchainCertificationListReqDto reqDto) {


        List<OmsOperationBlockchainCertificate> listOmsOperationBlockchainCertificate_1 = new ArrayList<>();
//步骤0: 6-1-02查询区块链证书列表 - queryBlockchainCertificationList
        List<OmsOperationBlockchainCertificate> listOmsOperationBlockchainCertificate = new ArrayList<>();
        QueryBlockchainCertificationListReq queryBlockchainCertificationListReq = new QueryBlockchainCertificationListReq();
        if (reqDto != null) {
            queryBlockchainCertificationListReq.setCertificateName(reqDto.getCertificateName());//SimpleFieldAssign//sourceId:989367_1
            queryBlockchainCertificationListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:989370_1
        }

        /*6-1-02查询区块链证书列表[6994]   */
        Assert.isNull(queryBlockchainCertificationListReq.getIsArchive(), "D6查询区块链证书列表-6-1-02查询区块链证书列表-是否存档不能为空", false);
        listOmsOperationBlockchainCertificate = mOmsOperationBlockchainCertificateService.queryBlockchainCertificationList(queryBlockchainCertificationListReq)/*vcase invoke 本地 method 方法调用;*/;


        listOmsOperationBlockchainCertificate_1 = listOmsOperationBlockchainCertificate;

        QueryBlockchainCertificationListRespDto retData = new QueryBlockchainCertificationListRespDto();
        retData.setDataDepositCertificationList(listOmsOperationBlockchainCertificate_1.stream().map(item -> BeanUtil.toBean(item, DataDepositCertificationDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:989381_1


        return retData;
    }

    /**
     * D6查询区块链证书详情[6998]
     * gen by moon at 5/30/2023, 7:09:59 PM
     */
    @Trace(operationName = "D6查询区块链证书详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBlockchainCertificationDetailRespDto queryBlockchainCertificationDetail(QueryBlockchainCertificationDetailReqDto reqDto) {


        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate_2 = null;
//步骤0: 6-1-02查询区块链证书详情 - queryBlockchainCertificationDetail
        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate = null;
        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate_1 = new OmsOperationBlockchainCertificate();
        if (reqDto != null) {
            omsOperationBlockchainCertificate_1.setBlockchainCertificateId(reqDto.getBlockchainCertificateId());//SimpleFieldAssign//sourceId:989382_1
        }

        /*6-1-02查询区块链证书详情[6995]   */
        Assert.isNull(omsOperationBlockchainCertificate_1.getBlockchainCertificateId(), "D6查询区块链证书详情-6-1-02查询区块链证书详情-区块链证书ID不能为空", false);
        omsOperationBlockchainCertificate = mOmsOperationBlockchainCertificateService.queryBlockchainCertificationDetail(omsOperationBlockchainCertificate_1)/*vcase invoke 本地 method 方法调用;*/;


        omsOperationBlockchainCertificate_2 = omsOperationBlockchainCertificate;

        QueryBlockchainCertificationDetailRespDto retData = new QueryBlockchainCertificationDetailRespDto();
        if (omsOperationBlockchainCertificate_2 != null) {
            retData.setBlockchainCertificateId(omsOperationBlockchainCertificate_2.getBlockchainCertificateId());//SimpleFieldAssign//sourceId:989401_1
            retData.setCertificateName(omsOperationBlockchainCertificate_2.getCertificateName());//SimpleFieldAssign//sourceId:989402_1
            retData.setCertificateCode(omsOperationBlockchainCertificate_2.getCertificateCode());//SimpleFieldAssign//sourceId:989403_1
            retData.setCertificateDesc(omsOperationBlockchainCertificate_2.getCertificateDesc());//SimpleFieldAssign//sourceId:989404_1
            retData.setDepositData(omsOperationBlockchainCertificate_2.getDepositData());//SimpleFieldAssign//sourceId:989405_1
            retData.setHashValue(omsOperationBlockchainCertificate_2.getHashValue());//SimpleFieldAssign//sourceId:989406_1
            retData.setShortHash(omsOperationBlockchainCertificate_2.getShortHash());//SimpleFieldAssign//sourceId:989407_1
            retData.setCertificateStartTime(omsOperationBlockchainCertificate_2.getCertificateStartTime());//SimpleFieldAssign//sourceId:989408_1
            retData.setCopyrightOwner(omsOperationBlockchainCertificate_2.getCopyrightOwner());//SimpleFieldAssign//sourceId:989409_1
            retData.setAuthorizationSpaceId(omsOperationBlockchainCertificate_2.getAuthorizationSpaceId());//SimpleFieldAssign//sourceId:989410_1
            retData.setSurfaceCode(omsOperationBlockchainCertificate_2.getSurfaceCode());//SimpleFieldAssign//sourceId:989411_1
            retData.setEntityId(omsOperationBlockchainCertificate_2.getEntityId());//SimpleFieldAssign//sourceId:989412_1
            retData.setThemeContentTypeCode(omsOperationBlockchainCertificate_2.getThemeContentTypeCode());//SimpleFieldAssign//sourceId:989413_1
            retData.setThemeContentId(omsOperationBlockchainCertificate_2.getThemeContentId());//SimpleFieldAssign//sourceId:989414_1
            retData.setSubjectLifeCycle(omsOperationBlockchainCertificate_2.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:989415_1
            retData.setOperationInductionId(omsOperationBlockchainCertificate_2.getOperationInductionId());//SimpleFieldAssign//sourceId:989416_1
            retData.setOperateTime(omsOperationBlockchainCertificate_2.getOperateTime());//SimpleFieldAssign//sourceId:989417_1
        }


        return retData;
    }

    /**
     * D6存档区块链证书[6999]
     * gen by moon at 5/30/2023, 7:10:00 PM
     */
    @Trace(operationName = "D6存档区块链证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ArchiveBlockchainCertificationRespDto archiveBlockchainCertification(ArchiveBlockchainCertificationReqDto reqDto) {


        //virtualUsage 6-1-02查询区块链证书详情  43358
        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate = null;
        OmsOperationBlockchainCertificate omsOperationBlockchainCertificate_1 = new OmsOperationBlockchainCertificate();
        if (reqDto != null) {
            omsOperationBlockchainCertificate_1.setBlockchainCertificateId(reqDto.getBlockchainCertificateId());//SimpleFieldAssign//sourceId:989418_1_43358
        }

        /*6-1-02查询区块链证书详情[6995]   */
        Assert.isNull(omsOperationBlockchainCertificate_1.getBlockchainCertificateId(), "D6存档区块链证书-6-1-02查询区块链证书详情-区块链证书ID不能为空", false);
        omsOperationBlockchainCertificate = mOmsOperationBlockchainCertificateService.queryBlockchainCertificationDetail(omsOperationBlockchainCertificate_1)/*vcase invoke 本地 method 方法调用;*/;


        if ((omsOperationBlockchainCertificate != null && omsOperationBlockchainCertificate.getSubjectLifeCycle() != null && omsOperationBlockchainCertificate.getSubjectLifeCycle().equals("CURRENTLY_VALID"))) {
            //if(6-1-02查询存证数据证书详情.主体生命周期 等于 当前有效)  43359

            boolean bOOLEAN;
            OmsOperationBlockchainCertificate omsOperationBlockchainCertificate_3 = new OmsOperationBlockchainCertificate();
            omsOperationBlockchainCertificate_3.setSubjectLifeCycle("ARCHIVING");//sourceId:989436_1_43360
            omsOperationBlockchainCertificate_3.setIsArchive("TRUE");//sourceId:989437_1_43360
            if (reqDto != null) {
                omsOperationBlockchainCertificate_3.setBlockchainCertificateId(reqDto.getBlockchainCertificateId());//SimpleFieldAssign//sourceId:989438_1_43360
            }

            /*6-1-02修改区块链证书[6996]   */
            Assert.isNull(omsOperationBlockchainCertificate_3.getBlockchainCertificateId(), "D6存档区块链证书-6-1-02修改区块链证书-区块链证书ID不能为空", false);
            Assert.isNull(omsOperationBlockchainCertificate_3.getSubjectLifeCycle(), "D6存档区块链证书-6-1-02修改区块链证书-主体生命周期不能为空", false);
            Assert.isNull(omsOperationBlockchainCertificate_3.getIsArchive(), "D6存档区块链证书-6-1-02修改区块链证书-是否存档不能为空", false);
            bOOLEAN = mOmsOperationBlockchainCertificateService.updateBlockchainCertification(omsOperationBlockchainCertificate_3)/*vcase invoke 本地 method 方法调用;*/;


        }
        ArchiveBlockchainCertificationRespDto retData = new ArchiveBlockchainCertificationRespDto();


        return retData;
    }

    /**
     * D6获取上链证书(公共)[7001]
     * gen by moon at 5/30/2023, 6:58:28 PM
     */
    @Trace(operationName = "D6获取上链证书(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBlockchainCertificationDetailComRespDto queryBlockchainCertificationDetailCom(QueryBlockchainCertificationDetailComReqDto reqDto) {


        QueryBlockchainCertificationRespDto obtainBlockchainCertificationRes_1 = null;
//步骤0: M6获取上链证书（特殊方法） - queryBlockchainCertification
        //ModelCode: obtainBlockchainCertification
        QueryBlockchainCertificationRespDto obtainBlockchainCertificationRes = null;
        QueryBlockchainCertificationReqDto obtainBlockchainCertificationReq = new QueryBlockchainCertificationReqDto();
        if (reqDto != null) {
            obtainBlockchainCertificationReq.setBlockchainKey(reqDto.getBlockchainKey());//SimpleFieldAssign//sourceId:989449_1
            obtainBlockchainCertificationReq.setBlockchainSecret(reqDto.getBlockchainSecret());//SimpleFieldAssign//sourceId:989450_1
            obtainBlockchainCertificationReq.setBlockchainDataUnicode(reqDto.getBlockchainDataUnicode());//SimpleFieldAssign//sourceId:989451_1
        }

        /*M6获取上链证书（特殊方法）[7000]   */
        Assert.isNull(obtainBlockchainCertificationReq.getBlockchainKey(), "D6获取上链证书(公共)-M6获取上链证书（特殊方法）-上链KEY不能为空", false);
        Assert.isNull(obtainBlockchainCertificationReq.getBlockchainSecret(), "D6获取上链证书(公共)-M6获取上链证书（特殊方法）-上链密钥不能为空", false);
        Assert.isNull(obtainBlockchainCertificationReq.getBlockchainDataUnicode(), "D6获取上链证书(公共)-M6获取上链证书（特殊方法）-存证数据唯一码不能为空", false);
        obtainBlockchainCertificationRes = nbBlockchain.queryBlockchainCertification(obtainBlockchainCertificationReq);


        obtainBlockchainCertificationRes_1 = obtainBlockchainCertificationRes;

        QueryBlockchainCertificationDetailComRespDto retData = new QueryBlockchainCertificationDetailComRespDto();
        if (obtainBlockchainCertificationRes_1 != null) {
            retData.setBlockchainDataUnicode(obtainBlockchainCertificationRes_1.getBlockchainDataUnicode());//SimpleFieldAssign//sourceId:989456_1
        }


        return retData;
    }
    //
}
